package com.wb;

import com.wb.entity.JpaTestBean;
import com.wb.repository.JpaTestDao;
import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : JpaJunit
 * @Author : 神经刀
 * @Date : 2019/6/11 : 16:58
 * @Version : 1.0
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class JpaJunit {

    private final Logger logger = LoggerFactory.getLogger(JpaJunit.class);

//    @Resource(name="jpaTestServer")
//    private IJpaTestService jpaTestService;

    @Resource(name = "jpaTestDao")
    private JpaTestDao jpaTestDao;

//    @Before
//    public void insertSql() {
//        List<JpaTestBean> jpaList = new ArrayList<JpaTestBean>(100);
//        JpaTestBean jpaTestBean = null;
//        for (int i = 0; i < 100; i++) {
//            jpaTestBean = new JpaTestBean();
//            jpaTestBean.setId(i);
//            jpaTestBean.setJpaName("name" + i);
//            jpaTestBean.setJpaAge(i);
//            ((ArrayList) jpaList).add(jpaTestBean);
//        }
//        List<JpaTestBean> result = jpaTestDao.save(jpaList);
//        logger.info(" reult: {} ", result);
//    }

    @Test
    public void say() {
//        jpaTestDao.queryByJpaName(new Pageable());
    }

    /**
     *  @author: 神经刀
     *  @Date: 2019/6/13 9:21
     *  @Description: 分页查询
     */
    @Test
    public void fenyeQuery() {
        Pageable pageable = new PageRequest(0, 10, new Sort(Sort.Direction.ASC, "id"));
        Page<JpaTestBean> result = null;
        try {
//            result = jpaTestDao.findById(1,pageable);
            JpaTestBean jpaTestBean = new JpaTestBean();
            jpaTestBean.setJpaName("name1");
            result = jpaTestDao.findByJpName(jpaTestBean, pageable);
        } catch (Exception e) {
            logger.error("" , e);
        }
        List<JpaTestBean> resultList = (List<JpaTestBean>)result.getContent();
        logger.info(" result : {}  " , result);
        for (JpaTestBean jpa: resultList) {
            logger.info("jpaTestBeanfor: {}  " , jpa);
        }
    }

    @Test
    public void update() {
        // JpaTestBean{id=11, jpaName='name11', jpaAge=11}
        JpaTestBean jpaTestBean = new JpaTestBean();
        jpaTestBean.setJpaName("name__123213112");
        jpaTestBean.setJpaAge(666);
        jpaTestBean.setId(11);
        try {
            JpaTestBean result = jpaTestDao.save(jpaTestBean);
            logger.info(" info : {}" , result);
        } catch (Exception e) {
            logger.error("" , e);
        }
    }

    @Test
    public void queryBenDi() {
        JpaTestBean jpaTestBean = new JpaTestBean();
        jpaTestBean.setId(11);
        List<JpaTestBean> resultList = jpaTestDao.findByJpaIdOnSql(jpaTestBean);
        logger.info("resultList : {} ", resultList);
    }
}
